//
//  AELNewDivingPointsController.swift
//  ActualExaminationLine
//
//  Created by iOS-Czz on 2023/10/30.
//

import UIKit
import WMZDialog
class AELNewDivingPointsController: AELBaseViewController {

    @IBOutlet var scoreTF: UITextField!
    @IBOutlet var dateTF: UITextField!
    @IBOutlet var locationTF: UITextField!
    @IBOutlet var nameTF: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.wx_navigationBar.isHidden = true
    }
    @IBAction func clickBackAction(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    @IBAction func clickSaveAction() {
        self.view.endEditing(true)
        if self.dateTF.text!.isEmpty {
            UIView.showTipsView("Please select score date")
            return
        }
        if self.nameTF.text!.isEmpty {
            UIView.showTipsView("Please enter score item")
            return
        }
        if self.locationTF.text!.isEmpty{
            UIView.showTipsView("Please enter location")
            return
        }
        if self.scoreTF.text!.isEmpty {
            UIView.showTipsView("Please select deducation of points")
            return
        }
        
        let model = AELPointsModel()
        model.name = self.nameTF.text!
        model.date = self.dateTF.text!
        model.location = self.locationTF.text!
        // 假设模型中 score 属性的类型是 Int
        if let scoreText = self.scoreTF.text, let scoreValue = Int(scoreText) {
            // 确保输入的文本不为空且可以转换为整数类型
            model.score = scoreValue
        } else {
            // 处理无效输入的逻辑，例如提示用户输入有效的分数
            print("Invalid input or empty text field")
        }
        model.isDefault = false
        model.saveToDB()
        UIView.showResultView("New driving points addition succeed", true)
        let helper = AELPointsModel.getUsingLKDBHelper()
        helper.delete(with: AELPointsModel.self, where: "isDefault = true")
        let delay = 1.5 // 2秒
        let dispatchTime = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
            self.navigationController?.popViewController(animated: true)
        }
    }
    @IBAction func clickSelectDateAction(_ sender: Any) {
        self.view.endEditing(true)
        let dialog = Dialog()
        dialog.wDateTimeTypeSet()("yyyy.MM.dd")
        .wDefaultDateSet()(Date())
        .wCancelTitleSet()("Cancel")
        .wOKTitleSet()("Ok")
        .wEventOKFinishSet()({(anyId:Any?,otherData:Any?)in
            self.dateTF.text = otherData as? String
        })
        .wTypeSet()(DialogTypeDatePicker);
        _ = dialog.wStart()
    }
    @IBAction func tapSelectScoreAction(_ sender: Any) {
        self.view.endEditing(true)
        let dialog = Dialog()
        dialog.wTypeSet()(DialogTypePickSelect)
            .wDataSet()(["0","1","2","3","4","5","6","7","8","9","10","11","12"])
            .wEventOKFinishSet()({(anyId:Any?,otherData:Any?)in
                self.scoreTF.text = String(format: "%@",(anyId as? String)!)
            })
            .wCancelTitleSet()("Cancel")
            .wOKTitleSet()("Ok")
        _ = dialog.wStart()
        
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
